package com.yizhuoyan.his.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yizhuoyan.his.qco.PatientCostsQco;
import com.yizhuoyan.his.qco.PatientCostsAo;
import com.yizhuoyan.his.ao.RegistrationFreeAo;
import com.yizhuoyan.his.dao.CaseRegistrationRecordDao;
import com.yizhuoyan.his.dao.FinPatientCostsDao;
import com.yizhuoyan.his.dao.HosRegistrationLevelDao;
import com.yizhuoyan.his.dto.ListCostDto;
import com.yizhuoyan.his.dto.QueryResult;
import com.yizhuoyan.his.entity.*;
import com.yizhuoyan.his.exception.HISException;
import com.yizhuoyan.his.service.financialstaff.RegistrationFeeService;
import com.yizhuoyan.his.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

/**
 * 患者费用查询相关服务
 */
@Service
@Transactional
public class RegistrationFeeServiceImpl implements RegistrationFeeService {
    @Autowired
    CaseRegistrationRecordDao cdao;
    @Autowired
    FinPatientCostsDao fdao;
    @Autowired
    HosRegistrationLevelDao hdao;

    @Override
    public ListCostDto queryPatientCostByCaseNo(@NotNull RegistrationFreeAo ao) throws Exception {
        //1.验证清理参数
        Long caseNo=Long.parseLong(Strings.trim(ao.getCaseNo()));
        if(caseNo==null){
            throw new HISException("RegistrationFreeFail","病例号不能为空");
        }
        //将字符串转为LocDateTime
        DateTimeFormatter df=DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm:ss");
        LocalDateTime starDate=null;
        LocalDateTime endDate=null;
       try{
           if(ao.getStartDate()!=null){
               starDate=LocalDateTime.parse(ao.getStartDate(),df);
           }

       } catch (Exception e){
           starDate=null;
       }
       try{
           if(ao.getEndDate()!=null){
               endDate=LocalDateTime.parse(ao.getEndDate(),df);
           }
       }catch (Exception e){
           endDate=null;
       }
       //分页
        Integer pageNo=parseInt(ao.getPageNo(),1);
        Integer pageSize=parseInt(ao.getPageSize(),10);
        if (pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize <= 0) {
            pageSize = 5;
        }

        //2.执行业务逻辑
        //2.0查询病历号是否存在
        if(cdao.exists("no",caseNo)){
            throw new HISException("RegistrationFreeFail","病历号不存在");
        }
        //2.1查询患者病历信息
        CaseRegistrationRecordEntity caseEntity=cdao.selectByCaseNO(caseNo);
        Long doctorLevelId=caseEntity.getRegisterDoctorLevel();
        //医生级别
        HosRegistrationLevelEntity levelEntity=hdao.selectById(doctorLevelId);

        //2.2查询费用详情
        Long id=caseEntity.getId();
        PatientCostsQco CostsAo=new PatientCostsQco();
        CostsAo.setCaseNo(id);
        CostsAo.setStartDate(starDate);
        CostsAo.setEndDate(endDate);
        List<FinPatientCostsEntity> list=fdao.selectByRegisteredKey(CostsAo);

        PageHelper.startPage(pageNo, pageSize);
        PageInfo<FinPatientCostsEntity> page=new PageInfo<>(list);

        //3.组装业务结果
        QueryResult<FinPatientCostsEntity> result=new QueryResult<FinPatientCostsEntity>();
        result.setPageNo(pageNo);
        result.setPageSize(pageSize);
        result.setRows(page.getList());
        result.setTotalRows(page.getTotal());
        ListCostDto dto=new ListCostDto();
        dto.setId(UUID.randomUUID().toString());
        dto.setCaseNo(caseEntity.getNo());
        dto.setRealName(caseEntity.getRealname());
        dto.setGender(caseEntity.getGender());
        dto.setIdCard(caseEntity.getIdcard());
        dto.setBirthDate(caseEntity.getBirthdate());
        dto.setAge(caseEntity.getAge());
        dto.setAgeType(caseEntity.getAgeType());
        dto.setHomeAddress(caseEntity.getHomeAddress());
        dto.setVisitDate(caseEntity.getVisitDate());
        dto.setVisitNoon(caseEntity.getVisitNoon());

        FinExpenseCategoryEntity categoryEntity=caseEntity.getSettleType();
        dto.setSettleType(categoryEntity.getName());
        dto.setRegisterTime(caseEntity.getRegisterTime());
        HosDepartmentEntity departmentEntity=caseEntity.getRegisterDepartment();
        dto.setDepartmentName(departmentEntity.getName());
        HosDoctorEntity doctorEntity=caseEntity.getRegisterDoctor();
        dto.setHosDoctor(doctorEntity.getRealName());
        dto.setPatientCots(result);
        return dto;
    }

    /**
     * 解析字符串为整数
     *
     * @param s
     * @param defaultInt
     * @return
     */
    public  Integer parseInt(String s, Integer defaultInt) {
        try {
            if (s == null){
                return defaultInt;
            }
            return Integer.parseInt(s);
        } catch (Exception e) {
            return defaultInt;
        }
    }
}
